package ynu.edu.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Entity
@Getter
@Setter
@DiscriminatorValue("2")
public class Customer extends User{
    @Column
    private String name;
    @OneToMany(mappedBy = "customer",
            fetch = FetchType.LAZY,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"customer"})
    private Set<Order1> orders;
    public Order1 cancelOrder(Order1 order){
        orders.remove(order);
        return order;
    }
    public List<Order1> findAllOrder(){
        return new ArrayList<>(orders);
    }
    public List<Order1> findOrderWithShop(Shop shop){
        List<Order1> order1List=new ArrayList<>();
        for(Order1 o:orders){
            if(Objects.equals(o.getShop().getId(), shop.getId())){
                order1List.add(o);
            }
        }
        return order1List;
    }
    public Order1 addOrder(Order1 order){
        orders.add(order);
        return order;
    }
    public Order1 fund(Order1 order){
        for(Order1 o:orders){
            if(Objects.equals(o.getId(), order.getId())){
                o.setStatus(1);
                return o;
            }
        }
        return null;
    }
}
